package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.basebean.BaseParam;
import com.cskaoyan.bean.vo.brand.WXBrandDetailVO;
import com.cskaoyan.bean.vo.brand.WXBrandListVO;
import com.cskaoyan.mapper.BrandMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.utils.CopyModuleClassUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;


/**
 * @description: ${description}
 * @author: cuicui
 **/
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    BrandMapper brandMapper;

    @Override
    public BrandVO brandList(BrandListBO bo) {
        BrandVO brandVO = new BrandVO();
        brandVO.setLimit(bo.getLimit());
        brandVO.setPage(bo.getPage());
        int total = (int) brandMapper.countByExample(null);
        brandVO.setTotal(total);
        int pages = ((int) Math.ceil(total * 1.0 / bo.getLimit()));
        brandVO.setPages(pages);
        if(bo.getName()!=null){
            bo.setName("%"+bo.getName()+"%");
        }
        List<Brand> brands = brandMapper.selectByBaseParam(bo);
        brandVO.setList(brands);
        return brandVO;
    }

    @Override
    public Brand creat( BrandCreateBO bo) {
        Brand record = new Brand();
        record.setName(bo.getName());
        record.setDesc(bo.getDesc());
        record.setPicUrl(bo.getPicUrl());
        record.setFloorPrice(new BigDecimal(bo.getFloorPrice()));
        Date date = new Date();
        record.setUpdateTime(date);
        record.setAddTime(date);
        int i = brandMapper.insertSelective(record);
        return record;
    }

    @Override
    public Brand update(Brand brand) {
        brand.setUpdateTime(new Date());
        int i = brandMapper.updateByPrimaryKeySelective(brand);
        Brand brand1 = brandMapper.selectByPrimaryKey(brand.getId());
        return brand1;
    }

    @Override
    public void delectLogic(Brand brand) {
        brand.setUpdateTime(new Date());
        brand.setDeleted(true);
        brandMapper.updateByPrimaryKeySelective(brand);
    }







    // By KunLin
    @Override
    public WXBrandDetailVO queryBrandDetailById(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return new WXBrandDetailVO().success(brand);
    }

    // By Kunlin
    @Override
    public WXBrandListVO queryBrandList(BrandListBO brandListBO) {
        PageHelper.startPage(brandListBO.getPage(), brandListBO.getLimit());

        BrandExample brandExample = new BrandExample();
        brandExample.createCriteria().andDeletedEqualTo(false);
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        WXBrandListVO wxBrandListVO = new WXBrandListVO();
        wxBrandListVO.setData(new WXBrandListVO.DataDTO());
        wxBrandListVO.getData().setLimit(brandListBO.getLimit());
        wxBrandListVO.getData().setPage(pageInfo.getPageNum());
        wxBrandListVO.getData().setTotal(pageInfo.getTotal());
        wxBrandListVO.getData().setPages(pageInfo.getPages());
        wxBrandListVO.getData().setList(new LinkedList<>());
        for (Brand brand : brands) {
            WXBrandListVO.DataDTO.ListDTO listDTO = CopyModuleClassUtils.bean2vo(brand, WXBrandListVO.DataDTO.ListDTO.class);
            wxBrandListVO.getData().getList().add(listDTO);
        }
        return wxBrandListVO.success();
    }
}
